<?php

namespace App\Model;

use houdunwang\arr\Arr;
use Illuminate\Database\Eloquent\Model;

class Classify extends Model
{
    protected $guarded=[];
    //找父级，这张表互相关联
    public function getFather(){
        return $this->belongsTo(Classify::class,'pid','id');
    }
    public function getSon(){
        return $this->hasMany(Classify::class,'id','pid');
    }
    //获得树状关系
    public function getTree(){
            $data=Classify::get()->toArray();
            $classify=Arr::tree($data, 'name', 'id', 'pid');
            return $classify;
    }
        public function getClass($model){
        $classify=$this->getTree();
        foreach ($classify as$item){
            if ($item['id']==$model['id']){
                $model=$item;
            }
        }
        foreach ($classify as$k=> $item){
            if ($item['_level']>=$model['_level']){
                $classify[$k]['_disabled']='disabled="disabled"';
            }else{
                $classify[$k]['_disabled']='';
            }
        }
        return $classify;
        }
        public function getChild($model){
        $son=Classify::where('pid',$model['id'])->first();
        if ($son){
           //表示含有子集
            return true;
        }else{
            return false;
        }
        }

}
